﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ThoiTrang.Business; 
using ThoiTrang.Entities;

public partial class Admin_adm_articleImgList_upload : System.Web.UI.Page
{
    public string aid = "";
    public string ckid = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            a_Session ss = null;
            a_User usr = null;
            a_UserType usrType = null;
            ss = a_SessionService.Geta_Session(SessionManager.GetCookie());
            ckid = ss.SessionID;
            usr = a_UserService.Geta_User(ss.CookieID);
            usrType = a_UserTypeService.Geta_UserType(usr.UserTypeID);

            aid = Request.QueryString["aid"];
         
            a_Article aa = a_ArticleService.Geta_Article(Convert.ToInt32(aid));
           

            string iid = Request.QueryString["iid"] == null ? "" : Request.QueryString["iid"];

            ltrImageName.Text = Request.QueryString["ta"];
            imgImageMain.ImageUrl="~/"+ FOConstants.Dir_ImgArticle+Request.QueryString["ta"] + "-l" + aid + ".jpg";
          
        }
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (fluImage.PostedFile != null)
        {
            if (fluImage.PostedFile.FileName != "")
            {
                if (fluImage.PostedFile.ContentLength > 0)
                {
                    char[] spl = { '/', '\\' };
                    string fileServer = Request.QueryString["ta"] + "-l" + aid + ".jpg";
                    string dirServer = Server.MapPath("~/" + FOConstants.Dir_ImgArticle).Trim(spl);
                    try
                    {
                        System.IO.File.Delete(dirServer + "\\" + fileServer);
                    }
                    catch { }
                    fluImage.SaveAs(dirServer + "\\" + fileServer);

                }
            }
        }
    }
    protected void btnGoBack_Click(object sender, EventArgs e)
    {
        aid = Request.QueryString["aid"];
        ckid = Request.QueryString["ckid"];

        a_Article aa = a_ArticleService.Geta_Article(Convert.ToInt32(aid));

        if (aa.IsFeatured)
        {
            Response.Redirect("~/admin/adm_article_list.asp?ckid="+ckid+"&st=1&isfea=1");
        }
        if (aa.PublishStatus)
        {
            Response.Redirect("~/admin/adm_article_list.asp?ckid=" + ckid + "&st=1");
        }
        else
        {
            Response.Redirect("~/admin/adm_article_list.asp?ckid=" + ckid + "&st=0");
        }
    }
    protected void btnDelete_Click(object sender, ImageClickEventArgs e)
    {
        aid = Request.QueryString["aid"];

        a_Article aa = a_ArticleService.Geta_Article(Convert.ToInt32(aid));
        char[] spl = { '/', '\\' };
        string fileServer = Request.QueryString["ta"]+"-l"+aid+".jpg";

        string dirServer = Server.MapPath("~/" + FOConstants.Dir_ImgArticle).Trim(spl);
        try
        {
            System.IO.File.Delete(dirServer + "\\" + fileServer);
        }
        catch { }
       
    }

    public string DetailImageName(object aID, object kTitle, object fYear, object imgNo)
    {
        string res = "";
        res = kTitle.ToString().ToLower().Trim().Replace(" ", "-");
        res = res + "-" + fYear.ToString() + "-l" + aID.ToString() + ".jpg";
        return res;
    }
}
